package bancosys.tec.datadigester.registry.impl;

import java.util.Collections;
import java.util.Map;

import bancosys.tec.datadigester.fileregistry.dao.FileRegistryDAO;
import bancosys.tec.datadigester.fileregistry.domain.DriverProperty;
import bancosys.tec.datadigester.fileregistry.domain.FileRegistry;
import bancosys.tec.datadigester.registry.DriverRegistry;
import bancosys.tec.datadigester.registry.DriverRegistryLoader;
import bancosys.tec.persist.controller.TransactionalController;
import bancosys.tec.persist.dao.BaseDAO;
import bancosys.tec.persist.dao.BeanNotFoundException;

/**
 * Implementação do carregador de registro de driver que lê as informações gravadas no banco.
 * 
 * @author piercio
 */
public class DatabaseRegistryLoader implements DriverRegistryLoader {

    private TransactionalController controller;

    /**
     * {@inheritDoc}
     */
    public DriverRegistry load() {
        DriverRegistry registry = new DriverRegistry();
        FileRegistryDAO dao = this.controller.getDAOFactory().getDAOByClass(FileRegistryDAO.class);
        for (FileRegistry file : dao.findAll()) {
            registry.register(file.getFileCode(), file.getDriverClass());
        }
        return registry;
    }

    /**
     * @param transactionalController o valor para o campo controller
     */
    public void setController(TransactionalController transactionalController) {
        this.controller = transactionalController;
    }

    /**
     * {@inheritDoc}
     */
    public Map<String, String> driverProperties(String fullQualifiedName) {
        BaseDAO<DriverProperty> dao = this.controller.getDAOFactory().getGenericDAO(DriverProperty.class);
        try {
            return dao.findByNaturalKey(fullQualifiedName).getProperties();
        } catch (BeanNotFoundException e) {
            Map<String, String> props = Collections.emptyMap();
            return props;
        }
    }

}
